<?php
include_once '../classes/design.php';
include_once '../classes/clienteServicos.php';
include_once '../../classes/erro.php';
include_once '../../classes/automovel.php';
include_once '../../classes/manipuladorXML.php';

// Salvar, se tiver pedido pra salvar
if (isset($_REQUEST["salvar"])) {
	$params["id"] = $_REQUEST["id"];
	$params["modelo_id"] = $_REQUEST["modelo_id"];
	$params["ano"] = $_REQUEST["ano"];
	$params["cor"] = $_REQUEST["cor"];
	$xml = ClienteServicos::requisitar("automoveis/salvar.php", $params);
	$automovel = ManipuladorXML::desserializar($xml);
	if (gettype($automovel) == "object" && get_class($automovel) == "Erro") {
		header("Location: index.php?erro=" . $automovel->get_msg());
		exit;
	}
	else {
		header("Location: index.php?sucesso=Automóvel " . $automovel->get_modelo()->get_nome() . " salvo!");
		exit;
	}
}

$xml = ClienteServicos::requisitar("marcas/get_todas.php");
$marcas = ManipuladorXML::desserializar($xml);

// Recuperar o automovel
if (isset($_REQUEST["id"])) {
	$xml = ClienteServicos::requisitar("automoveis/get.php", array("id" => $_REQUEST["id"]));
	$automovel = ManipuladorXML::desserializar($xml);
	if (gettype($automovel) == "object" && get_class($automovel) == "Erro") {
		header("Location: index.php?erro=" . $automovel->get_msg());
		exit;
	}
}
else {
	$automovel = new Automovel();
	$automovel->set_ano(date("Y"));
}

$design = new Design("..");
$design->imprimir_topo();

echo "
	<h1>Automóveis</h1>
";

if ($automovel->get_id() == -1) {
	echo "<h2>Novo automóvel</h2>";
}
else {
	echo "<h2>Alterar automóvel</h2>";
}

echo "
	<form method=\"post\" action=\"" . $_SERVER["PHP_SELF"] . "\">
		<input type=\"hidden\" name=\"id\" value=\"" . $automovel->get_id() . "\" />
		<input type=\"hidden\" name=\"salvar\" value=\"1\" />
		<table border=\"0\" align=\"center\">
			<tr>
				<td>Modelo
					<select name=\"modelo_id\">
";
foreach ($marcas as $marca) {
	echo "
						<optgroup label=\"" . $marca->get_nome() . "\">
	";
	$xml = ClienteServicos::requisitar("modelos/get_todos.php", array("marca_id" => $marca->get_id()));
	$modelos = ManipuladorXML::desserializar($xml);
	foreach ($modelos as $modelo) {
		echo "
							<option value=\"" . $modelo->get_id() . "\"";
		if ($automovel->get_modelo() == $modelo) {
			echo " selected";
		}
		echo ">" . $modelo->get_nome() . "</option>";
	}
	echo "
						</optgroup>
	";
}

echo "
					</select>
					Ano
					<select name=\"ano\">
";
for ($ano = date("Y") + 1; $ano >= date("Y") - 15; $ano--) {
	echo "
						<option value=\"$ano\"";
	if ($ano == $automovel->get_ano()) {
		echo " selected";
	}
	echo ">$ano</option>";
}
echo "
					</select>
					Cor
					<input type=\"text\" name=\"cor\" value=\"" . $automovel->get_cor() . "\"/>
				</td>
			</tr>
			<tr>
				<td align=\"center\">
					<button type=\"submit\">Salvar</button>
					<button type=\"button\" onclick=\"javascript: location.href='index.php';\">Cancelar</button>
				</td>
			</tr>
		</table>
	</form>
";

$design->imprimir_rodape();

?>